﻿//======================================================================
//
//        Copyright (C) 2007-2008 三月软件工作室    
//        All rights reserved
//
//        filename :Connections
//        description :
//
//        created by 侯垒 at  2009-11-14 15:26:43
//        http://houleixx.cnblogs.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Data.Common;

namespace Entity.Core
{
    /// <summary>
    /// 配置文件中所有的连接字符串
    /// </summary>
    public class Connections
    {

        /// <summary>
        /// 添加数据库连接
        /// </summary>
        /// <param name="model">数据库实体</param>
        /// <returns></returns>
        public bool Add(ConnectionModel model)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(ConnectionFile.FilePath);
            DataRow row = ds.Tables[0].NewRow();
            row[0] = model.ConnectionString;
            row[1] = model.DataBaseName;
            row[2] = model.Provider;
            ds.Tables[0].Rows.Add(row);
            try
            {
                ds.WriteXml(ConnectionFile.FilePath);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 删除数据库连接
        /// </summary>
        /// <param name="model">要删除的连接实体</param>
        /// <returns>删除成功返回true,否则返回false</returns>
        public bool Delete(ConnectionModel model)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(ConnectionFile.FilePath);
            DataRow delrow = ds.Tables[0].NewRow();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (row[0].ToString() == model.ConnectionString && row[1].ToString() == model.DataBaseName && row[2].ToString() == model.Provider)
                {
                    delrow = row;
                    break;
                }
            }
            try
            {
                ds.Tables[0].Rows.Remove(delrow);
                ds.WriteXml(ConnectionFile.FilePath);
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// 得到所有连接字符
        /// </summary>
        /// <returns></returns>
        public IList<ConnectionModel> GetConnectionStrings()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(ConnectionFile.FilePath);
            IList<ConnectionModel> list = new List<ConnectionModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ConnectionModel model = new ConnectionModel();
                model.ConnectionString = row[0].ToString();
                model.DataBaseName = row[1].ToString();
                model.Provider = row[2].ToString();
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 检测连接字符串是否可以连接成功
        /// </summary>
        /// <param name="conn"></param>
        /// <returns></returns>
        public bool CheckConnection(ConnectionModel model)
        {
            DbProviderFactory factory=DbProviderFactories.GetFactory(model.Provider);
            using (IDbConnection conn = factory.CreateConnection())
            {
                conn.ConnectionString = model.ConnectionString;
                try
                {
                    conn.Open();
                    if (conn.State == ConnectionState.Open)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
